package com.bite.partition;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.util.Random;
import java.util.function.Supplier;

@SpringBootApplication
public class RabbitPartitionProducerApplication {

	private static final Random RANDOM = new Random(System.currentTimeMillis());

	private static final String[] data = new String[] {
			"abc1", "abc2", "abc3",
			"abc4"
	};

	public static void main(String[] args) {
		new SpringApplicationBuilder(RabbitPartitionProducerApplication.class)
				.web(WebApplicationType.NONE)
				.run(args);
	}

	@Bean
	public Supplier<Message<?>> generate() {
		return () -> {
			String value = data[RANDOM.nextInt(data.length)];
			System.out.println("Sending: " + value);
			return MessageBuilder.withPayload(value)
					.setHeader("partitionKey", value)
					.build();
		};
	}

}
